package com.xxq.semester.controller.utils;

import com.xxq.semester.dto.VerificationDTO;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.utils.VerificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
@RestController
@RequestMapping("/api/verification")
@Tag(name="验证码管理", description = "提供验证码发送和验证功能")
public class VerificationController {
    private static final Logger logger = LoggerFactory.getLogger(VerificationController.class);
    @Autowired
    private VerificationService verificationService;
    @Operation(summary = "发送验证码", description = "向指定邮箱发送验证码")
    @PostMapping("/send")
    public ResponseEntity<String> sendCode(@RequestParam("email") String email) {
        String code = verificationService.generateAndSaveCode(email);
        verificationService.sendVerificationCode(email, code);
        logger.info("验证码已发送,{}",code);
        // 发送验证码的逻辑
        return ResponseEntity.success("验证码已发送", null);
    }
    @Operation(summary = "验证验证码", description = "验证用户输入的验证码是否正确")
    @PostMapping("/auth")
    public ResponseEntity<String> register(@RequestBody VerificationDTO verificationDTO) {
        if (!verificationService.verifyCode(verificationDTO.getEmail(), verificationDTO.getVerificationCode())) {
            logger.error("验证码错误");
            return ResponseEntity.error("验证码错误");
        }
        logger.info("验证码正确");
        return ResponseEntity.success("验证码正确",null);
    }

}
